/**
 * 如果缓存的字段数据与 vuex 相同，更新时建议从 vuex 进行更新，并同步到缓存
 */

import { isEmpty, getUrlParams, serializeToUrl } from '@common/utils.js';

/**
 * 清除 localStorage 存储
 * * 以"\_"开头的key会被保留，不会被删除；如：_userRole
 * @param {boolean} isAll 是否删除所有，包括以"_"开头的属性
 */
export function clearStorage(isAll = false) {
  for (let i = 0; i < localStorage.length; i++) {
    const key = localStorage.key(i);
    if (!isAll && (isEmpty(key) || key.indexOf('_') === 0)) continue;

    localStorage.removeItem(key);
    i--;
  }
}

function removeStorage(name) {
  localStorage.removeItem(name);
  return true;
}

function setStorage(name, value) {
  localStorage.setItem(name, value);
  return true;
}

function getStorage(name, defaultVal) {
  return localStorage.getItem(name) || defaultVal;
}

/**
 * 是否正在登录
 * @type {string}
 */
const IsLoginProcess = 'isLoginProcess';

export function getIsLoginProcess() {
  return getStorage(IsLoginProcess) === 'true';
}

export function setIsLoginProcess(value) {
  return setStorage(IsLoginProcess, value);
}

/**
 * 直连某个路由。可以跟随参数。需使用encodeURIComponent编码
 * * 例如：encodeURIComponent('/user/user-info?ie=UTF-8&wd=ip')
 * @type {string}
 */
const Direct = 'direct';

export function removeDirect() {
  return removeStorage(Direct);
}

export function getDirect() {
  return getStorage(Direct);
}

export function setDirect(value) {
  return setStorage(Direct, value);
}

/**
 * token
 * @type {string}
 */
const Token = 'Authorization';

export function getToken() {
  return {
    key: Token,
    value: getStorage(Token)
  };
}

export function setToken(token) {
  return setStorage(Token, token);
}

/**
 * 同token
 * @type {string}
 */
const XTenantHeader = 'x-tenant-header';

export function getXTenantHeader() {
  return {
    key: XTenantHeader,
    value: getStorage(XTenantHeader)
  };
}

export function setXTenantHeader(value) {
  return setStorage(XTenantHeader, value);
}

/**
 * url参数上的placeId
 * @type {string}
 */
const PlaceId = '_placeId';

export function getPlaceId() {
  return getStorage(PlaceId);
}

export function setPlaceId(value) {
  return setStorage(PlaceId, value);
}

/**
 * 微信公众号的biz，用于未关注公众号跳转的
 * @type {string}
 */
const MpFlag = '_mpFlag';

export function getMpFlag() {
  return getStorage(MpFlag);
}

export function setMpFlag(value) {
  return setStorage(MpFlag, value);
}

/**
 * 微信配置信息
 * @type {string}
 */
const WxConfig = 'wxConfig';

export function getWxConfig() {
  let data = {};

  try {
    data = JSON.parse(getStorage(WxConfig));
  } catch (error) {
    return {};
  }

  return data;
}

export function setWxConfig(value) {
  return setStorage(WxConfig, JSON.stringify(value));
}

/**
 * 用户账号列表信息
 * @description 一个微信可能存在多个用户账号
 * @type {string}
 */
const UserInfos = 'userInfos';

export function getUserInfos() {
  let data = [];

  try {
    data = JSON.parse(getStorage(UserInfos));
  } catch (error) {
    return [];
  }

  return data;
}

export function setUserInfos(value) {
  return setStorage(UserInfos, JSON.stringify(value));
}

/**
 * 用户信息
 * @type {string}
 */
const UserInfo = 'userInfo';

export function getUserInfo() {
  let data = {};

  try {
    data = JSON.parse(getStorage(UserInfo));
  } catch (error) {
    return {};
  }

  return data;
}

export function setUserInfo(value) {
  return setStorage(UserInfo, JSON.stringify(value));
}

/**
 * 用户微信信息
 * @type {string}
 */
const UserWx = 'userWx';

export function getUserWx() {
  let data = {};

  try {
    data = JSON.parse(getStorage(UserWx));
  } catch (error) {
    return {};
  }

  return data;
}

export function setUserWx(value) {
  return setStorage(UserWx, JSON.stringify(value));
}

/**
 * 用户角色列表
 * @type {string}
 */
const UserRoles = 'userRoles';

export function getUserRoles() {
  let data = {};

  try {
    data = JSON.parse(getStorage(UserRoles));
  } catch (error) {
    return {};
  }

  return data;
}

export function setUserRoles(value) {
  return setStorage(UserRoles, JSON.stringify(value));
}

/**
 * 当前选中的角色，即当前角色
 * @type {string}
 */
const UserRole = '_userRole';

export function getUserRole() {
  let data = {};

  try {
    data = JSON.parse(getStorage(UserRole));
  } catch (error) {
    return {};
  }

  return data;
}

export function setUserRole(value) {
  return setStorage(UserRole, JSON.stringify(value));
}

/**
 * 首页选择角色的弹窗只显示一次
 * @type {string}
 */
const HomeIsShowRolePopup = '_homeIsShowRolePopup';

export function getHomeIsShowRolePopup() {
  return getStorage(HomeIsShowRolePopup);
}

export function setHomeIsShowRolePopup(value) {
  return setStorage(HomeIsShowRolePopup, value);
}

export const getWxConfigUrl = () => {
  const { href, origin, pathname } = location;
  const url = href.split('#')[0];
  const { code, state, placeId, ...param } = getUrlParams(url, false);

  // 需要其他参数在地址栏显示的在这里添加加
  return `${origin}${pathname}?${serializeToUrl({ placeId })}`;
};
